package com.yhp.vx.controller;

import com.yhp.bean.Courier;
import com.yhp.bean.Message;
import com.yhp.bean.User;
import com.yhp.mvc.ResponseBody;
import com.yhp.service.impl.CourierService;
import com.yhp.service.impl.UserService;
import com.yhp.util.GsonUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

public class VXLoginController {

    @ResponseBody("/vx/login.do")
    public String login(HttpServletRequest request, HttpServletResponse response){
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        Courier courier = CourierService.findByName(userName);
        User user = UserService.findByName(userName);
        Message msg = new Message();
        Date date = new Date();
        if(courier != null && userName.equals(courier.getName()) && password.equals(courier.getPassWord())){
            msg.setStatus(0);
            msg.setResult("登陆成功"+userName);
            CourierService.updateTime(date,userName);
            request.getSession().setAttribute("user",courier);
            request.getSession().setAttribute("userInfo",0);

        }else if(user != null && userName.equals(user.getName()) && password.equals(user.getPassWord())){
            msg.setStatus(1);
            msg.setResult("登陆成功"+userName);
            UserService.updateTime(date,userName);
            request.getSession().setAttribute("user",user);
            request.getSession().setAttribute("userInfo",1);
        }else {
            msg.setStatus(-1);
            msg.setResult("登陆失败,账号或密码不正确");
        }
        String s = GsonUtil.toJSON(msg);
        return s;
    }

    @ResponseBody("/vx/userInfo.do")
    public String userInfo(HttpServletRequest request, HttpServletResponse response){
        Object userInfo = request.getSession().getAttribute("userInfo");
        Message msg = new Message();
        msg.setStatus((Integer) userInfo);
        String s = GsonUtil.toJSON(msg);
        return s;
    }


    @ResponseBody("/vx/outLogin.do")
    public String outLogin(HttpServletRequest request, HttpServletResponse response){
        request.getSession().invalidate();
        Object userName = request.getSession().getAttribute("userName");
        System.out.println(userName);
        Message msg = new Message();
        if(userName == null){
            msg.setStatus(0);
        }else {
            msg.setStatus(-1);
        }
        System.out.println(msg.getStatus());
        String s = GsonUtil.toJSON(msg);
        return s;
    }


    @ResponseBody("/vx/addGo.do")
    public String addLogin(HttpServletRequest request, HttpServletResponse response){
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        String userPhone = request.getParameter("userPhone");
        String numberId = request.getParameter("numberId");
        User u = new User();
        u.setNumberId(numberId);
        u.setName(name);
        u.setPhone(userPhone);
        u.setPassWord(password);
        boolean insert = UserService.insert(u);
        Message msg = new Message();
        if (insert) {
            msg.setResult("添加成功");
            msg.setStatus(0);
        } else {
            msg.setResult("添加失败");
            msg.setStatus(-1);
        }
        String s = GsonUtil.toJSON(msg);
        System.out.println(s);
        return s;
    }
}
